package Action;


import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import Form.RegisterUserForm;
import Model.user;
import Service.userService;
/**
 * 注册动作类
 * @author MacBook
 *
 */
public class RegisterAction extends ActionSupport implements ModelDriven<RegisterUserForm>{
	private RegisterUserForm registerform = new RegisterUserForm(); 
	
	public RegisterUserForm getRegisterform() {
		return registerform;
	}

	public void setRegisterform(RegisterUserForm registerform) {
		this.registerform = registerform;
	}

	@Override
	public RegisterUserForm getModel() {
		// TODO Auto-generated method stub
		return registerform;
	}
	//ajax验证用户名是否存在
	public void veriUser(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		userService veriUsername = new userService();
		boolean exist = veriUsername.veriUser(username);
		HttpServletResponse response = ServletActionContext.getResponse();
		//写回客户端
		try {
			response.getWriter().print(exist+"");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//action执行
	public String execute(){
		//提交表格之后执行注册服务
		userService register = new userService();
		user u = register.RegisterService(registerform);
		if(u != null)
		{
			//注册成功后放入session里
			ActionContext.getContext().getSession().put("user", u);
			return SUCCESS;
		}
		else
			return ERROR;
	}
	

}
